/*************************************************
Author: will
Date: 9/12/2012
Description:
Notes: 
*************************************************/

#ifndef __CTaskTargetStat_h__
#define __CTaskTargetStat_h__

#include "CTaskID.h"
#include "CTaskCond.h"

class CTaskTargetStat
{
public:
	CTaskTargetStat() 
    {
	}

	virtual ~CTaskTargetStat() 
    {
	}

    int Init(CTaskTargetStatData& stData)
    {
        int iRet = 0;

        _bIsReachCond = (stData._ucIsReachCond == 1);
        iRet = _stTaskID.Init(stData._stTaskID);
        CHECK_RETURN(iRet);

        iRet = _stCond.Init(stData._stCond);
        CHECK_RETURN(iRet);

        iRet = _stNowStat.Init(stData._stNowStat);
        CHECK_RETURN(iRet);

        _iData1 = stData._iData1;

        return 0;
    }

    int GetData(CTaskTargetStatData& stData)
    {
        int iRet = 0;
    
        stData._ucIsReachCond = _bIsReachCond ? 1 : 0;

        iRet = _stTaskID.GetData(stData._stTaskID);
        CHECK_RETURN(iRet);

        iRet = _stCond.GetData(stData._stCond);
        CHECK_RETURN(iRet);

        iRet = _stNowStat.GetData(stData._stNowStat);
        CHECK_RETURN(iRet);

        stData._iData1 = _iData1;

        return 0;
    }

    int Trigger(CTaskCond& stCond, bool & bTaskTargetStatusChange)
    {
        CT_TRACE(("任务状态更新:"));

        if (_stCond._ucCond != stCond._ucCond)
        {
            CT_TRACE(("任务状态更新事件类型不符"));
            return 0;
        }

        switch(stCond._ucCond)
        {
        case CTaskCond::TASK_NONE:
            break;
        case CTaskCond::TASK_LEVEL_UP:
            if (_stNowStat._iCondData1 <= stCond._iCondData1)
            {
                _stNowStat._iCondData2 = 1;
                bTaskTargetStatusChange = true;
            }
            break;
        case CTaskCond::TASK_PET_LEVEL_UP:
            if (_stNowStat._iCondData1 <= stCond._iCondData1)
            {
                _stNowStat._iCondData2 = 1;
                bTaskTargetStatusChange = true;
            }
            break;
        case CTaskCond::TASK_TASK_FINISH:
            {
                CTaskID stCondTask;
                stCondTask._uiBranchID =  stCond._iCondData1 / TASK_CONFIG_ID_MOD;
                stCondTask._uiTaskID = stCond._iCondData1 % TASK_CONFIG_ID_MOD;

                CTaskID stNowTask;
                stNowTask._uiBranchID =  _stNowStat._iCondData1 / TASK_CONFIG_ID_MOD;
                stNowTask._uiTaskID = _stNowStat._iCondData1 % TASK_CONFIG_ID_MOD;

                if (stCondTask._uiBranchID == stNowTask._uiBranchID
                    && stCondTask._uiTaskID >= stNowTask._uiTaskID)
                {
                    _stNowStat._iCondData2 = 1;
                    bTaskTargetStatusChange = true;
                }
            }
            break;
        case CTaskCond::TASK_BOSS_CONQUER:
        case CTaskCond::TASK_ENEMY_KILL:
        case CTaskCond::TASK_USE_ITEM:
        case CTaskCond::TASK_GET_ITEM:
        case CTaskCond::TASK_GET_AND_COMMIT_ITEM:
            {
                if (stCond._iCondData1 == _stNowStat._iCondData1)
                {
                    _stNowStat._iCondData2 += stCond._iCondData2; //次数
                    bTaskTargetStatusChange = true;
                }
            }            
            break;
        case CTaskCond::TASK_FINISH_STAGE:
            {
                if (stCond._iCondData1 / CONST_TASK_INSTANCE_ID_MOD == _stNowStat._iCondData1 / CONST_TASK_INSTANCE_ID_MOD)
                {
                    if (stCond._iCondData1 % CONST_TASK_INSTANCE_ID_MOD >= _stNowStat._iCondData1 % CONST_TASK_INSTANCE_ID_MOD)
                    {
                        _stNowStat._iCondData2 += stCond._iCondData2; //次数
                        bTaskTargetStatusChange = true;
                    }
                }
            }
            break;
        case CTaskCond::TASK_DO_SOME_OPT:
            {
                if (_stNowStat._iCondData1 / 1000 == TASK_OPT_ID_113 
                    && stCond._iCondData1 / 1000 == TASK_OPT_ID_113)
                {
                    if (stCond._iCondData1 % 1000 >= _stNowStat._iCondData1 % 1000)
                    {
                        _stNowStat._iCondData2 += stCond._iCondData2; //次数
                        bTaskTargetStatusChange = true;
                    }
                }
                else if (_stNowStat._iCondData1 / 1000 / 1000 == TASK_OPT_ID_116 
                            && stCond._iCondData1 / 1000 / 1000 == TASK_OPT_ID_116) 
                {
                    if (stCond._iCondData1 / 1000 == _stNowStat._iCondData1 / 1000
                        && (stCond._iCondData1 % 1000 >= _stNowStat._iCondData1 % 1000))
                    {
                        _stNowStat._iCondData2 += stCond._iCondData2;
                        bTaskTargetStatusChange = true;
                    }
                }
                else if (_stNowStat._iCondData1 / 1000 == TASK_OPT_ID_124 
                    && stCond._iCondData1 / 1000 == TASK_OPT_ID_124) 
                {
                    if (stCond._iCondData1 % 1000 >= _stNowStat._iCondData1 % 1000)
                    {
                        _stNowStat._iCondData2 += stCond._iCondData2;
                        bTaskTargetStatusChange = true;
                    }
                }
                else
                {
                    if (stCond._iCondData1 == _stNowStat._iCondData1)
                    {
                        _stNowStat._iCondData2 += stCond._iCondData2; //次数
                        bTaskTargetStatusChange = true;
                    }
                }
            }
            break;
        default:
            return 0;
        }

        CT_TRACE(("事件: 类型 %d , Data1 %d, Data2 %d", 
            stCond._ucCond, stCond._iCondData1, stCond._iCondData2));
        CT_TRACE(("更新后状态：类型 %d , Data1 %d, Data2 %d", 
            _stNowStat._ucCond, _stNowStat._iCondData1, _stNowStat._iCondData2));

        //CheckReachCond();

        return 0;
    }

    bool IsReachCond()
    {
        return _bIsReachCond;
    }

    int CheckReachCond()
    {
        if (CTaskCond::IsReachCond(_stCond, _stNowStat))
        {
            _bIsReachCond = true;
        }
        else
        {
            _bIsReachCond = false;
        }

        return 0;
    }

public:
    bool            _bIsReachCond;
    CTaskID         _stTaskID;
    CTaskCond       _stCond;
    CTaskCond       _stNowStat;

    int             _iData1;
};


#endif // __CTaskTargetStat_h__

